package com.vibrationkeyboard.app.utils

import android.util.Log
import java.text.SimpleDateFormat
import java.util.*

object Logger {
    
    private const val TAG = "VibrationKeyboard"
    private const val MAX_TAG_LENGTH = 23
    private val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault())
    
    fun d(message: String, tag: String = TAG) {
        if (BuildConfig.DEBUG) {
            Log.d(truncateTag(tag), formatMessage(message))
        }
    }
    
    fun i(message: String, tag: String = TAG) {
        Log.i(truncateTag(tag), formatMessage(message))
    }
    
    fun w(message: String, tag: String = TAG) {
        Log.w(truncateTag(tag), formatMessage(message))
    }
    
    fun e(message: String, throwable: Throwable? = null, tag: String = TAG) {
        Log.e(truncateTag(tag), formatMessage(message), throwable)
    }
    
    fun v(message: String, tag: String = TAG) {
        if (BuildConfig.DEBUG) {
            Log.v(truncateTag(tag), formatMessage(message))
        }
    }
    
    private fun formatMessage(message: String): String {
        val timestamp = dateFormat.format(Date())
        return "[$timestamp] $message"
    }
    
    private fun truncateTag(tag: String): String {
        return if (tag.length > MAX_TAG_LENGTH) {
            tag.substring(0, MAX_TAG_LENGTH)
        } else {
            tag
        }
    }
    
    // 特定模块的日志方法
    fun sensor(message: String) {
        d(message, "Sensor")
    }
    
    fun processor(message: String) {
        d(message, "Processor")
    }
    
    fun recognition(message: String) {
        d(message, "Recognition")
    }
    
    fun ui(message: String) {
        d(message, "UI")
    }
    
    fun service(message: String) {
        d(message, "Service")
    }
    
    fun data(message: String) {
        d(message, "Data")
    }
    
    fun wear(message: String) {
        d(message, "Wear")
    }
} 